//
// Created by 周振 on 2018/8/10.
//

#include "com_zzhou_common_library_util_NativeHelper.h"
#include <jni.h>
#include <string.h>
#include <stdio.h>
#include <android/log.h>
#include "md5.h"

JNIEXPORT jstring JNICALL Java_com_zzhou_common_library_util_NativeHelper_MD5
        (JNIEnv *env, jclass jclass1, jstring str) {
    char *szText = (char *) (*env)->GetStringUTFChars(env, str, 0);

    MD5_CTX context = {0};
    MD5Init(&context);
    MD5Update(&context, szText, strlen(szText));
    unsigned char dest[16] = {0};
    MD5Final(dest, &context);
    (*env)->ReleaseStringUTFChars(env, str, szText);

    int i = 0;
    char szMd5[32] = {0};
    for (i = 0; i < 16; i++) {
        sprintf(szMd5, "%s%02x", szMd5, dest[i]);
    }

    return (*env)->NewStringUTF(env, szMd5);
}

JNIEXPORT jstring JNICALL
Java_com_zzhou_common_library_util_NativeHelper_encrypt(JNIEnv *env, jclass type, jstring data_) {
    jclass clzz = (*env)->FindClass(env, "com/zzhou/common/library/util/CipherUtils");
    if (clzz == NULL) {
        __android_log_write(ANDROID_LOG_DEBUG, "cc", "clzz is null");
    }
    jmethodID encryptId = (*env)->GetStaticMethodID(env, clzz, "encrypt",
                                                    "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;");
//    const jchar *ckey = "LMQx0%ttF@GmXeJKXUlQkwf!";
    const jchar *ckey = "EDUCLOUDEDUCLOUDEDUCLOUD";
    jstring value = (*env)->NewStringUTF(env, ckey);
    jstring ditmsg = (jstring) (*env)->CallStaticObjectMethod(env, clzz, encryptId, data_, value);
    return ditmsg;
}

JNIEXPORT jstring JNICALL
Java_com_zzhou_common_library_util_NativeHelper_decrypt(JNIEnv *env, jclass type, jstring data_) {
    jclass clzz = (*env)->FindClass(env, "com/zzhou/common/library/util/CipherUtils");
    if (clzz == NULL) {
        __android_log_write(ANDROID_LOG_DEBUG, "cc", "clzz is null");
    }
    jmethodID decryptId = (*env)->GetStaticMethodID(env, clzz, "decrypt",
                                                    "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;");
//    const jchar *ckey = "LMQx0%ttF@GmXeJKXUlQkwf!";
    const jchar *ckey = "njxtqgjyptfromlianchuang";
    jstring value = (*env)->NewStringUTF(env, ckey);
    jstring ditmsg = (jstring) (*env)->CallStaticObjectMethod(env, clzz, decryptId, data_, value);
    return ditmsg;
}
JNIEXPORT jstring JNICALL
Java_com_zzhou_common_library_util_NativeHelper_newEncrypt(JNIEnv *env, jclass type, jstring data_) {
    jclass clzz = (*env)->FindClass(env, "com/zzhou/common/library/util/CipherUtils");
    if (clzz == NULL) {
        __android_log_write(ANDROID_LOG_DEBUG, "cc", "clzz is null");
    }
    jmethodID encryptId = (*env)->GetStaticMethodID(env, clzz, "encrypt",
                                                    "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;");
    const jchar *ckey = "4#Z%KA7I4!8Nj4TDkNzKyuyy";
    jstring value = (*env)->NewStringUTF(env, ckey);
    jstring ditmsg = (jstring) (*env)->CallStaticObjectMethod(env, clzz, encryptId, data_, value);
    return ditmsg;
}

JNIEXPORT jstring JNICALL
Java_com_zzhou_common_library_util_NativeHelper_newDecrypt(JNIEnv *env, jclass type, jstring data_) {
    jclass clzz = (*env)->FindClass(env, "com/zzhou/common/library/util/CipherUtils");
    if (clzz == NULL) {
        __android_log_write(ANDROID_LOG_DEBUG, "cc", "clzz is null");
    }
    jmethodID decryptId = (*env)->GetStaticMethodID(env, clzz, "decrypt",
                                                    "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;");
    const jchar *ckey = "4#Z%KA7I4!8Nj4TDkNzKyuyy";
    jstring value = (*env)->NewStringUTF(env, ckey);
    jstring ditmsg = (jstring) (*env)->CallStaticObjectMethod(env, clzz, decryptId, data_, value);
    return ditmsg;
}